package com.iaz.tech.tools.ai.neuron.cell;

import com.iaz.tech.tools.ai.neuron.ability.ICorrespondAbility;
import com.iaz.tech.tools.ai.neuron.ability.INeuronAbility;
import com.iaz.tech.tools.ai.neuron.ability.INeuronLifecycleAble;
import com.iaz.tech.tools.ai.neuron.runtime.INeuronRuntimeAbility;
import com.iaz.tech.tools.message.IMessageAble;
import com.iaz.tech.tools.tasks.ability.ITaskItemAble;

import lombok.Data;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 神经元
 */
@Data
public abstract class Neuron implements INeuronAbility, INeuronLifecycleAble, ICorrespondAbility, ITaskItemAble {
	/**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(Neuron.class);
	/* Neuron 的ID 号， 利用long 进行处理 */
	private UUID neuronId ; 
	/* 神经元的名称 */
	private String neuronName = "";	
	/**
	 * 设置图标
	 */
	private boolean sleepTitle = true;
	/**
	 * 睡眠时间
	 */
	private long sleepTitleTime = 0l;
	/**
	 * 如果sleepTile 为True, timeForSleep 的值为<=0 则需要手动唤醒
	 */
	private long timeoutForSleep = 0L;
	private long sleepTime = 0L;
	/**
	 * 神经元的Runtime及世界
	 */
	private INeuronRuntimeAbility runtime = null;
	/**
	 * 当前时间
	 */
	private long currentTime = 0L;
	
	public Neuron() {
		/**
		 * 获得UUID
		 */
		this.neuronId = UUID.randomUUID();
	}
	
	/**
	 * 证明是活跃
	 */
	@Override
	public boolean life() {
		return true;
	}
	/**
	 * 释放神经元
	 */
	@Override
	public void release() {
		
	}
	/**
	 * 证明是否是活跃的
	 */
	@Override
	public boolean isActive() {
		return !this.sleepTitle;
	}	
	/**
	 * 一旦调用Sleep 则需要手动苏醒
	 */
	@Override
	public void sleep() {
		try {
			this.sleepTitle = true;
			this.sleepTitleTime = (new java.util.Date()).getTime();
			this.timeoutForSleep = 0L;
		} catch(Exception e) {
			logger.error("sleep 出现异常：" + e.getMessage());
		}		
	}
	/**
	 * 一般是停止服务一段时间，默认为10分钟
	 */
	@Override
	public void await(long timeout) {
		try {
			this.sleepTitle = true;
			this.sleepTitleTime = (new java.util.Date()).getTime();
			this.timeoutForSleep = timeout;
		} catch(Exception e) {
			logger.error("sleep 出现异常：" + e.getMessage());
		}		
	}
	/**
     * 唤醒
     */
	@Override
	public void arouse(){
		try {
			this.sleepTitle = false;
			this.sleepTitleTime = 0L;
			this.timeoutForSleep = 0L;
		} catch(Exception e) {
			logger.error("sleep 出现异常：" + e.getMessage());
		}
	}
	/**
	 * 内部会话
	 */
	@Override
	public IMessageAble talkMe(IMessageAble message) {
		//throw new Exception("本函数不支持");
		return null;
	}	
	@Override
	public long thisItemID() { 
		return this.getNeuronId().hashCode();
	}
	@Override
	public long forExecuteTime() {
		 return this.getCurrentTime();
	}	
	@Override
	public UUID forNeuronID() {
		return this.neuronId;
	}

	@Override
	public void pushRuntime(INeuronRuntimeAbility my_runtime) {
		this.setRuntime(my_runtime);
	}	
}
